//
//  HomeMenuView.m
//  DingDongXiChe
//
//  Created by wtjr on 2018/1/4.
//  Copyright © 2018年 xieyingze. All rights reserved.
//

#import "HomeMenuView.h"
#import "SwitchButton.h"
#import "HomeServiceListCell.h"

@interface HomeMenuView()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UIView *mainView;

@property (nonatomic, strong) SwitchButton *switchButton;

@property (nonatomic, strong) BaseLabel *currentLocationLabel;

@property (nonatomic, strong) UITableView *tableView;

@end

@implementation HomeMenuView

- (void)initData {
    self.mainView.layer.cornerRadius = kUI_LayerCorner;
}

- (void)initSubViews {
    [self addSubview:self.mainView];
}

- (void)autoLayoutSubViews {
    [self.mainView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(@0);
    }];
}

- (void)bindViewModel {
    @weakify(self);
    self.switchButton.clickCompleted = ^(NSInteger index) {
        @strongify(self);
        [self.viewModel.updateMenuViewLayoutSubject sendNext:@(index)];
        [self updateListViewAndLocationLabel:index];
    };
}

#pragma mark - BaseMethod

/**
 切换服务列表和当前位置的显示/隐藏
 */
- (void)updateListViewAndLocationLabel:(NSInteger)index {
    self.tableView.hidden = index == 0 ? YES : NO;
    self.currentLocationLabel.hidden = index == 0 ? NO : YES;
    if (index == 1) [self.tableView reloadData];
}

#pragma mark - Interface


#pragma mark - Delegate

#pragma mark - UITableViewDataSource
//返回行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.viewModel.annotationModelLists.count;;
}

//返回Cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HomeServiceListCell *cell = [HomeServiceListCell cellWithTableView:tableView indexPath:indexPath];
    cell.cellLineType = kCellLineTypeNormal;
    cell.model = self.viewModel.annotationModelLists[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    [self.viewModel.gotoServiceDetailSubject sendNext:self.viewModel.annotationModelLists[indexPath.row]];
}

#pragma mark - TouchEvent


#pragma mark - Setter

- (void)setViewModel:(HomeViewModel *)viewModel {
    _viewModel = viewModel;
    @weakify(self);
    [self.viewModel.updateCurrentLocationSubject subscribeNext:^(id  _Nullable x) {
        @strongify(self);
        self.currentLocationLabel.text = (NSString *)x;
    }];
}

#pragma mark - Getter

- (UIView *)mainView {
    if (!_mainView) {
        _mainView = [UIView new];
        _mainView.backgroundColor = kCOLOR_white;
        [_mainView setViewShadow:kCOLOR_shadow offset:CGSizeMake(0, 0) radius:kUI_ShawRadius];
        
        SwitchButton *switchButton = [SwitchButton new];
        switchButton.leftTitle = @"位置";
        switchButton.rightTitle = @"列表";
        self.switchButton = switchButton;
        [_mainView addSubview:switchButton];
        [switchButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.right.equalTo(@0);
            make.height.equalTo(@40);
        }];
        
        [_mainView addSubview:self.currentLocationLabel];
        [self.currentLocationLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(switchButton.mas_bottom).offset(10);
            make.left.equalTo(@20);
            make.height.equalTo(@20);
        }];
        
        [_mainView addSubview:self.tableView];
        [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(switchButton.mas_bottom).offset(10);
            make.left.right.equalTo(@0);
            make.bottom.equalTo(@-10);
        }];
        self.tableView.hidden = YES;
    }
    return _mainView;
}

- (BaseLabel *)currentLocationLabel {
    if (!_currentLocationLabel) {
        _currentLocationLabel = [BaseLabel new];
        [_currentLocationLabel setLabelStyle:@"定位中..." textColor:KCOLOR_mainColor textFont:kFONT_mainNumBoldFont(12) texrAlignment:NSTextAlignmentLeft];
    }
    return _currentLocationLabel;
}

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [UITableView new];
        _tableView.backgroundColor = kCOLOR_tableView;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.rowHeight = 54;
    }
    return _tableView;
}


@end
